<?php
/**
 * Created by PhpStorm.
 * User: Fan
 * Date: 2018/7/23
 * Time: 20:25
 */

namespace Modules\Shop\Service;
use Blade;

class TagService
{
    //创建自定义标签
    public function make()
    {
        //创建分类标签
        $this::category();
        $this::article();
        $this::slider();
        $this::comment();
        $this::link();
    }
    /**
     * 创建友情链接
     */
    private static function link(){
        Blade::directive('tolink',function ($canshu){
            $canshu = $canshu ? $canshu : '[]';
            //定义定界符字符串
            $php = <<<php
            <?php
            \$params = $canshu;
            //获取所有的分类数据
            \$db = new \Modules\Admin\Entities\Link();
            //如果存在自定义标签参数，才加上条件
            if(isset(\$params['show'])){
               \$db = \$db->where('show',\$params['show']);
            }else{
                \$db = \$db->where('show',1);
            }
            \$link = \$db->get();
            foreach(\$link as \$li){
            
            ?>
php;
            return $php;
        });
        Blade::directive('endtolink',function (){
            $php = <<<php
            <?php
                }
            ?>
php;
            return $php;
        });
    }

    /**
     * 创建分类标签方法
     */
    private static function category()
    {
        //自定义模板标签
        //第一个参数：自定义标签的名字
        //第二个参数：回调函数【自定义标签的执行动作由回调函数决定】
        Blade::directive('category', function ($canshu) {
            $canshu = $canshu ? $canshu : '[]';
            //定义定界符字符串
            $php = <<<php
            <?php
            \$params = $canshu;
            //获取所有的分类数据
            \$db = new \Modules\Shop\Entities\Category();
            //如果存在自定义标签参数，才加上条件
            if(isset(\$params['pid'])){
               \$db = \$db->where('pid',\$params['pid']);
            }
            \$data = \$db->get();
            foreach(\$data as \$v){
            //判断当前点击的标题按钮
            \$v['url'] = '/shop/work/' . \$v['id'];
            
            ?>                      
php;
            return $php;
        });
        Blade::directive('endcategory', function () {
            $php = <<<php
            <?php
                }
            ?>
php;
            return $php;
        });
    }

    /**
     * 文章标签
     */
    private static function article()
    {
        Blade::directive('article', function ($canshu) {
            $canshu = $canshu ?: '[]';
            $php = <<<php
            <?php
                \$params = $canshu;
                \$db = new \Modules\Shop\Entities\Article();
                //如果存在cid的值，就代表需要获取某个分类下的文章
                if (isset(\$params['cid'])){
                    \$db = \$db->where('category_id',\$params['cid']);
                }
                //如果存在iscommend值，就取推荐文章
                if (isset(\$params['iscommend'])){
                    \$db = \$db->where('iscommend',\$params['iscommend']);
                }
                //如果有paginate值就分页
                //if (isset(\$params['paginate'])){
                //    \$db = \$db->paginate(\$params['paginate']);
                //}
                //如果存在click值，就按照点击数排序
                if (isset(\$params['click'])){
                    \$db = \$db->orderBy('click','desc');
                }
                //如果存在limit值，就截取传递的limit条数，如果不存在，就取默认的条数
                if (isset(\$params['limit'])){
                    \$db = \$db->limit(\$params['limit']);
                }else{
                    \$db = \$db->limit(5);
                }
                \$article = \$db->get();
                foreach(\$article as \$a){
                    //处理文章点击跳转地址
                    \$a['url'] = '/shop/single' . \$a['id'];
            ?>
php;
            return $php;
        });
        Blade::directive('endarticle', function () {
            $php = <<<php
            <?php
                }   
            ?>
php;
            return $php;
        });
    }

    /**
     * 轮播图标签
     */
    private static function slider(){
        Blade::directive('slider',function($canshu){
            $canshu = $canshu ?: '[]';
            $php = <<<php
            <?php
                \$params = $canshu;
                \$db = new \Modules\Slider\Entities\Slider();
                //如果存在cid的值，就代表需要获取某个分类下的文章
                if (isset(\$params['is_show'])){
                    \$db = \$db->where('is_show',\$params['is_show']);
                }else{
                    \$db = \$db->where('is_show',1);
                }
                if (isset(\$params['is_group'])){
                    \$db = \$db->where('is_group',\$params['is_group']);
                }else{
                    \$db = \$db->where('is_group',1);
                }
                //获取后台轮播图数据
                \$slider = \$db->get();
                foreach(\$slider as \$s){
            ?>
php;
            return $php;
        });
        Blade::directive('endslider',function(){
            $php = <<<php
                <?php
                    }
                ?>
php;
            return $php;
        });
    }

    /**
     * 留言标签
     */
    private static function comment(){
        Blade::directive('comment',function(){
            $php = <<<php
                \$db = \Modules\Shop\Entities\Comment()->where('status',1);
php;

        });
    }
}
